<?php

namespace App\Http\Requests\Cart;

use App\Http\Requests\ApiRequest;
use App\Models\Cart\Cart;
use App\Models\Products\Product;
use App\Services\Utils\ApiChecker;
use Illuminate\Validation\Validator;


/**
 * @property string product_id
 * @property array|null option
 * @property string quantity
 * @property string|null session_id
 */
class CartStoreRequest extends ApiRequest
{
    use ApiChecker, ProductOptionRequest;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * @return array
     *
     * @throws \Exception
     */
    public function rules()
    {
        return [
            'language'   => 'required|string|max:' . self::MAX_STRING,
            'store_id'   => 'nullable|int|min:0|max:' . self::MAX_INT,
            'session_id' => 'nullable|string|size:' . Cart::SESSION_ID_LENGTH,
            'currency'   => 'required|string|max:' . self::MAX_STRING,
            'product_id' => 'required|int|min:1|max:' . self::MAX_INT,
            'quantity'   => 'required|int|min:1',
            'option'     => 'sometimes|required|array',
            'option.*'   => 'required',
            'option.*.*' => 'required',
        ];
    }
    
    /**
     * @param Validator $validator
     * @throws \Exception
     */
    protected function afterValidator(Validator $validator)
    {
        $this->checkProductExist();
        $this->checkProductQuantity();
        $this->checkProductOptions();
    }
}
